/**
 * 费用管理初始化
 */
var CdcExpense = {
    id: "CdcExpenseTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};



/**
 * 初始化表格的列
 */
CdcExpense.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '费用ID', field: 'id', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '就诊ID', field: 'episodeId', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '总费', field: 'costs', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '自付费', field: 'pays', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '费用类型', field: 'busExpenseType.description', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '业务数据类型', field: 'data.description', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '是否有效', field: 'isActive', align: 'center', valign: 'middle', sortable: true,width:100},
        //{title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',
        //     formatter:function(value,row,index){
    	// 	var _html = '<a href="javascript:CdcExpense.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
    	// 	_html += '<a href="javascript:CdcExpense.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
    	// 	return _html;
        // }
        //}
        ];
};

/**
 * 检查是否选中
 */
CdcExpense.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CdcExpense.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增费用
 */
CdcExpense.add = function () {
    var index = Sys.open('新增费用',Sys.backendPath + '/cdc/cdcexpense/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看费用详情
 */
CdcExpense.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('费用详情',Sys.backendPath + '/cdc/cdcexpense/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除费用
 */
CdcExpense.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/cdc/cdcexpense/delete", function (data) {
	            Sys.success("删除成功!");
	            CdcExpense.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("cdcExpenseId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除费用?",operation);
    }
};

/**
 * 查询费用列表
 */
CdcExpense.search = function () {
    var queryData = {};
    queryData['costs'] = $("#costs").val();
    queryData['pays'] = $("#pays").val();
    queryData['busExpenseTypeId'] = $("#busExpenseTypeId").val();
    queryData['dataId'] = $("#dataId").val();
    queryData['isActive'] = $("#isActive").val();
    CdcExpense.table.refresh({query: queryData});
};

CdcExpense.query = function () {
    if ((EpisodeIDTable.selEpisode)&&(EpisodeIDTable.selEpisode.id)) {
        var queryData=queryData = '{"NotQuery":"false","episodeId":"' + EpisodeIDTable.selEpisode.id + '"}';
        queryData = JSON.parse(queryData);
        CdcExpense.table.clear();
        CdcExpense.table.refresh({ query: queryData });
    }else{
        // CdcExamination.table.clear();
        Sys.info('请先选择患者！' );
    }
};



CdcExpense.ClickRow=function (r,e,f){

    CdcExpitem.expenseId=r.id;
    CdcExpitem.table.clear();
    CdcExpitem.table.refresh({query: {expenseId:r.id}});
    // alert("111");
};

$(function () {
    var defaultColunms = CdcExpense.initColumn();
    var table = new BSTable(CdcExpense.id, Sys.backendPath + "/cdc/cdcexpense/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(20);
    table.setShowRefresh(false);
    table.setShowColumns(false);
    table.setShowToggle(false);
    table.setShowExport(false);
    table.setSubHead(80);
    table.setQueryParams({ NotQuery: true });
    table.setOnClickRow(CdcExpense.ClickRow);

    CdcExpense.table = table.init();
    CdcExpense.query();

});
